var util = require('../../utils/util.js');

//获取应用实例
var app = getApp()
Page({
  data: {
    item  : {
       display     : 'hide',
       error_info  : '手机号格式不正确',
    },
    get_veritify_btn:{
       css_class:'default',
       text:'获取验证码'
    },
    button :{
       disabled:'false'
    },
    mobile:'',
    veritify:'',
    sms_veritify:'',
  },
  
  changeMobileInput:function(e){
      this.data.mobile = e.detail.value;
      this.isActionBindBtn();
  },

  changeVeritifyInput:function(e){
      this.data.veritify = e.detail.value;
      this.isActionBindBtn();
  },
  
  //验证用户的输入信息，判断是否激活绑定按钮
  isActionBindBtn:function(){
    var mobile   =  this.data.mobile;
    var veritify =  this.data.veritify;
   
    if (veritify.length == 4 && util.isMobile(mobile)){
        var disabled = 'true'; 
    }else{
        var disabled = 'false';
    }

    this.setData({
      button: {
        disabled: disabled
      },
    })
  },
  
  //点击获取验证码按钮
  getVeritify:function(){
    var that = this;
    var session_id = wx.getStorageSync('session_id');
    if (this.data.get_veritify_btn.css_class == 'default'){
      //判断手机号格式是否正确
      var mobile = this.data.mobile;
      if(util.isMobile(mobile)){
        wx.request({
          url: app.data.servser + '/login/getVerifity',
          data: {
            session_id: session_id,
            mobile: mobile,
          },
          success: function (res) {
            if(res.data.status == 1){
              that.data.sms_veritify = res.data.verify;
            }
          }
        })
         this.countDownGetVeritify();
      } else {
        this.setData({
          item: {
            display: 'show',
            error_info: '手机号格式不正确',
          },
        });
        this.closeErrorInfo();
      }
    }

  },

  goRechargePage:function(){
     wx.navigateTo({ url: '/pages/recharge/recharge' })
  },

  //用户绑定验证码操作
  doBindMobile:function(){
    var that = this;
    var session_id = wx.getStorageSync('session_id');
    if (that.data.button.disabled == 'true'){
      this.doRequest();
  
      if (that.data.veritify != '' && that.data.veritify == that.data.sms_veritify){
        wx.request({
          url: app.data.servser + '/login/bindMobile',
          data: {
            session_id: session_id,
            mobile: that.data.mobile,
          },
          success: function (res) {
            if (res.data.status == 1) {
               wx.setStorage({
                 key: 'mobile',
                 data: that.data.mobile
               });
               wx.navigateBack(1);
            }else{
              that.setData({
                item: {
                  display: 'show',
                  error_info: '绑定失败，请稍后重试',
                },
              });
              that.closeErrorInfo();
            }
          }
        })
      }else{
        this.setData({
          item: {
            display: 'show',
            error_info: '验证码输入不正确',
          },
        });
        this.closeErrorInfo();
      }
    }
  },

  countDownGetVeritify:function(){
     var that = this;
     var begin_time = 60;
     that.setData({
       get_veritify_btn: {
         css_class: 'getting',
         text: '倒计时 60 s'
       },
     });
     var begin_time = 59;
     this.changeVeritifyBtn(begin_time)
  },

  changeVeritifyBtn:function(time){
    var that = this;
    setTimeout(function(){
      that.setData({
        get_veritify_btn: {
          css_class: 'getting',
          text: '倒计时 ' + time + ' s'
        },
      });
      if(time>=1){
        time -=1;
        that.changeVeritifyBtn(time);
      }else{
        setTimeout(
          function () {
            that.setData({
              get_veritify_btn: {
                css_class: 'default',
                text: '重新获取验证码'
              }
            })
          }, '1000')
      }
    },'1000')
  },

  doRequest:function(){
    this.setData({
      button: {
        disabled: 'false'
      },
    })

    wx.showLoading({
       title: '绑定中',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)
  },

  //定时器关闭提示信息
  closeErrorInfo: function () {
    var that = this;
    setTimeout(function () { that.setData({ item: { display: 'hide' } }) }, '2000')
  },
})
